package kr.co.doriclassic.powerjava.chapter12;

import java.util.Arrays;
import java.util.Comparator;
//Object 형이 아니라 Student 형으로 정렬하고 싶다.
class Student{
	private String stdName;
	private double gpa;
	public Student(String stdName, double gpa) {
		this.stdName = stdName;
		this.gpa = gpa;
	}
	
	public String getStdName() {
		return stdName;
	}
	public double getGpa() {
		return gpa;
	}
	@Override
	public String toString() {
		return String.format("Student [%s, %s]", stdName, gpa);
	}
	/*@Override
	public int compareTo(Student arg0) {
		//오름차순 정렬
		//return (int)(this.gpa-arg0.gpa);
		//내림차순 정렬
		return (int)(arg0.gpa-this.gpa);
	}	*/
}


/**정렬
 * @author M201_06
 *
 */
public class SortTest {
	public static void main(String[] args) {
		int[] arr = {5,6,7,8,1,2,3,4};
		//배열 정렬 API, comparable 인터페이스 호출 필요
		Arrays.sort(arr);
		
		
		//prrArr(arr);		
		
		Student[] arrStd = new Student[4];
		
		arrStd[0] = new Student("홍길동", 3.00);
		arrStd[1] = new Student("임꺽정", 4.00);
		arrStd[2] = new Student("황진이", 2.00);
		arrStd[3] = new Student("김태희", 1.00);
		//Arrays.sort(arrStd);//1번방법
		//prrArr(arrStd);
		//문자열을 비교할 때
				Comparator<Student> compName = new Comparator<Student>() {
			
			@Override
			public int compare(Student o1, Student o2) {
				//해쉬코드=글자를 숫자로 변경, 문자열 비교시 반드시 따라옴
				return o1.getStdName().hashCode()-o2.getStdName().hashCode();
			}
		};
		//평점순 정렬
		Comparator<Student> compGpa = new Comparator<Student>() {
			
			@Override
			public int compare(Student o1, Student o2) {
				//해쉬코드=글자를 숫자로 변경
				return (int)(o1.getGpa()*10-o2.getGpa()*10);
			}
		};
		
		Arrays.sort(arrStd, compName);
		prrArr(arrStd);
		Arrays.sort(arrStd, compGpa);
		prrArr(arrStd);
	}

	private static void prrArr(Student[] arrStd) {
		for (Student s : arrStd) {
			System.out.print(s+"\t");
		}
		System.out.println();
	}

	/*private static void prrArr(int[] arr) {
		for (int e : arr) {
			System.out.printf("%2d",e);
		}
		System.out.println();
	}	*/
}
